package com.xd.guessmusic.myui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import com.xd.guessmusic.R;
import com.xd.guessmusic.model.IWordButtonClickListener;
import com.xd.guessmusic.model.WordButton;
import com.xd.guessmusic.util.Util;

import java.util.ArrayList;

/**
 * 自定义GridView，存放文字按钮
 * Created by DGF on 2015/5/14.
 * 观察者模式中的观察者
 */
public class MyGridView extends GridView {

    /**
     * 文字按钮的个数
     */
    public final static int COUNT_WORDS = 24;

    private Context context = null;
    private ArrayList<WordButton> wordButtons = new ArrayList<>();
    private MyGridAdapter adapter;
    private Animation scaleAnim;

    /**
     * 点击按钮的事件
     */
    private IWordButtonClickListener iWordButtonClickListener;

    /**
     * 注册监听事件
     * @param listener
     */
    public void setOnWordButtonClick(IWordButtonClickListener listener){
        this.iWordButtonClickListener = listener;
    }

    /**
     * 如果需要放到布局文件中，则需要实现该构造方法；
     * 如果不需要，只需实现一个参数的构造方法即可
     * @param context
     * @param attrs
     */
    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.context = context;

        adapter = new MyGridAdapter();
        this.setAdapter(adapter);
    }

    /**
     * 刷新数据
     * @param list
     */
    public void updateData(ArrayList<WordButton> list){
        wordButtons = list;
        this.setAdapter(adapter);
    }

    class MyGridAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return wordButtons.size();
        }

        @Override
        public Object getItem(int position) {
            return wordButtons.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            WordButton wordButton = null;
            scaleAnim = null;

            /**
             * 在convertView中设置样式（setLayoutParams），第一个item异常
             */
//            if (convertView == null){
//                convertView = Util.getView(context, R.layout.self_ui_gridview_item);
//
//                /**
//                 * 加载动画
//                 */
//                scaleAnim = AnimationUtils.loadAnimation(context, R.anim.scale);
//                /**
//                 * 为动画设置延迟时间，已保证每个按钮的动画都是在前一个按钮播放动画后播放
//                 */
//                scaleAnim.setStartOffset(position*100);
//
//                wordButton = wordButtons.get(position);
//                wordButton.setIndex(position);
//                wordButton.setButton((Button)convertView.findViewById(R.id.btn_item));
//
//                /**
//                 * 调用点击事件
//                 */
//                final WordButton finalWordButton = wordButton;
//                wordButton.getButton().setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (iWordButtonClickListener != null){
//                            iWordButtonClickListener.onWordButtonClick(finalWordButton);
//                        }
//                    }
//                });
//
//                convertView.setTag(wordButton);
//            }else{
//                wordButton = (WordButton)convertView.getTag();
//            }
//
//            wordButton.getButton().setText(wordButton.getText());
//
//            /**
//             * 不能在getView中修改样式，会导致第一个item点击异常
//             */
////            wordButton.getButton().setLayoutParams(new LayoutParams(80,80));
//
//            /**
//             * 播放动画
//             */
//            convertView.setAnimation(scaleAnim);
//
//            return convertView;

            View view = Util.getView(context, R.layout.self_ui_gridview_item);

            /**
             * 加载动画
             */
            scaleAnim = AnimationUtils.loadAnimation(context, R.anim.scale);
            /**
             * 为动画设置延迟时间，已保证每个按钮的动画都是在前一个按钮播放动画后播放
             */
            scaleAnim.setStartOffset(position*100);

            wordButton = wordButtons.get(position);
            wordButton.setIndex(position);
            wordButton.setButton((Button)view.findViewById(R.id.btn_item));
            wordButton.getButton().setText(wordButton.getText());
            wordButton.getButton().setLayoutParams(new LayoutParams(80,80));

            /**
             * 调用点击事件
             */
            final WordButton finalWordButton = wordButton;
            wordButton.getButton().setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iWordButtonClickListener != null){
                        iWordButtonClickListener.onWordButtonClick(finalWordButton);
                    }
                }
            });

            view.setTag(wordButton);

            return view;
        }
    }
}
